#define _KEY_MODULE_

#include "os_obj.h"
#include "key.h"
#include "pincfg.h"
#include "sys_init.h"
#include "bsp.h"
#include "bsp_hal.h"
#include "sys_ext.h"
#include "common.h"

/*******************************************************************************
* @brief  sysinit_end
* \param[in] pos : number
* \retval: none
*******************************************************************************/
void KeyTask(void const * argument)
{
  key_st_handle(&key, 1);
  
  if(key.found_edge & (1 << 1))
  {
    dbg_puts("key 1 press >> \n");
  }
  if(key.found_edge & (1 << 2))
  {
    dbg_puts("key 2 press >> \n");
  }
  if(key.found_edge & (1 << 3))
  {
    dbg_puts("key 3 press >> \n");
  }
  if(key.found_edge & (1 << 4))
  {
    dbg_puts("key 4 press >> \n");
  }
  
  if(key.lost_edge & (1 << 1))
  {
    dbg_puts("key 1 release >> \n");
  }
  if(key.lost_edge & (1 << 2))
  {
    dbg_puts("key 2 release >> \n");
  }
  if(key.lost_edge & (1 << 3))
  {
    dbg_puts("key 3 release >> \n");
  }
  if(key.lost_edge & (1 << 4))
  {
    dbg_puts("key 4 release >> \n");
  }
}

/*******************************************************************************
* @brief  key_init
* \param[in] pos : number
* \retval: none
*******************************************************************************/
void key_isr_read(void)
{
  uu16 krd = 0;
  
  if(!KEY1)
  krd |=  1 << 1;
  if(!KEY2)
  krd |= 1 << 2;
  if(!KEY3)
  krd |= 1 << 3;
  if(!KEY4)
  krd |= 1 << 4;
  
  key_st_sample(&key, 12, krd);
}


/*******************************************************************************
* @brief  key_init
* \param[in] pos : number
* \retval: none
*******************************************************************************/
void key_init(char pos)
{
  intx_alloc();
    
  intx_disable();
  
  vtask_list_add(&app_tick_task_list, key_isr_read);
  
  intx_enable();
}
OBJ_INIT_APP_EXPORT(key_init);


